package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.yc.sole.erp.dto.MoldDto;
import com.yc.sole.erp.dto.ProductDto;
import com.yc.sole.erp.exception.ErpResultEnum;
import com.yc.sole.erp.repository.MoldRepository;
import com.yc.sole.erp.repository.ProductRepository;
import com.yc.sole.erp.request.ProductQueryReq;
import com.yc.sole.erp.request.ProductReq;
import com.yc.sole.erp.response.ProductResp;
import com.yc.sole.framework.exception.BaseException;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.ConfigDto;
import com.yc.sole.system.repository.ConfigRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-12-15
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ProductBiz {

    private final ProductRepository productRepository;
    private final MoldRepository moldRepository;
    private final ConfigRepository configRepository;

    /**
     * 新增
     *
     * @param productReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveProduct(ProductReq productReq) {
        ProductDto productDto = productRepository.selectByIdDetail(productReq.getMoldId(), productReq.getColor(), productReq.getMaterial(), productReq.getSize(), productReq.getLength());
        if (Objects.nonNull(productDto)) {
            throw new BaseException(ErpResultEnum.PROD_NAME_REPEAT, productReq.getProductName());
        }
        MoldDto moldDto = moldRepository.selectMoldById(productReq.getMoldId());
        if (Objects.isNull(moldDto)) {
            throw new BaseException(ErpResultEnum.MOLD_NOT_FOUND);
        }
        ProductDto dto = new ProductDto();
        BeanUtil.copyProperties(productReq, dto);
        dto.setCategoryName(moldDto.getCategoryName());
        dto.setUnit(moldDto.getUnit());
        dto.setMoldName(moldDto.getMoldName());
        Long productId = productRepository.saveProduct(dto);
        productRepository.updateStock(productId, productReq.getStock(), "新增库存");
    }

    /**
     * 修改
     *
     * @param productReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProductById(ProductReq productReq) {
        ProductDto productDto = productRepository.selectByIdDetail(productReq.getMoldId(), productReq.getColor(), productReq.getMaterial(), productReq.getSize(), productReq.getLength());
        if (Objects.nonNull(productDto) && !productDto.getId().equals(productReq.getId())) {
            throw new BaseException(ErpResultEnum.PROD_NAME_REPEAT, productReq.getProductName());
        }
        ProductDto dto = new ProductDto();
        BeanUtil.copyProperties(productReq, dto);
        MoldDto moldDto = moldRepository.selectMoldById(productReq.getMoldId());
        if (Objects.isNull(moldDto)) {
            throw new BaseException(ErpResultEnum.MOLD_NOT_FOUND);
        }
        dto.setCategoryName(moldDto.getCategoryName());
        dto.setMoldName(moldDto.getMoldName());
        productRepository.updateProductById(dto);
        productRepository.updateStock(productDto.getId(), productReq.getStock() - productDto.getStock(), "产品库存");
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteProduct(Collection<Long> ids) {
        productRepository.deleteProductByIds(ids);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param productPageReq
     * @return
     */
    public Paging<ProductResp> selectProductPage(Paging<ProductQueryReq> page, ProductQueryReq productPageReq) {
        Paging<ProductDto> paging = productRepository.selectProductPage(page, productPageReq);
        Paging<ProductResp> productRespPaging = PageUtil.pageCopy(paging, ProductResp.class);
        if (CollUtil.isEmpty(productRespPaging.getRecords())) {
            return productRespPaging;
        }
        ConfigDto configDto = configRepository.selectConfig();
        for (ProductResp productResp : productRespPaging.getRecords()) {
            BigDecimal taxRate = new BigDecimal(String.valueOf(configDto.getTaxRate()));
            BigDecimal tax = productResp.getUnitPrice().multiply(taxRate).divide(new BigDecimal("100"), RoundingMode.HALF_UP);
            productResp.setUnTaxPrice(productResp.getUnitPrice().subtract(tax));
            productResp.setTaxRate(taxRate);
        }
        return productRespPaging;
    }

    /**
     * 通过id查询
     *
     * @param productId
     * @return
     */
    public ProductResp selectProductById(Long productId) {
        return BeanUtil.copyProperties(productRepository.selectProductById(productId), ProductResp.class);
    }

    /**
     * 通过名称查询
     *
     * @param productName
     * @return
     */
    public List<ProductResp> selectProductByName(String productName) {
        return BeanUtil.copyToList(productRepository.selectProductByName(productName), ProductResp.class);
    }
}
